﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Reflection;
using System.Windows.Ink;

namespace SmartBoard.Ink.PenCreator
{
    public partial class PenCreatorDialog
    {
        private static readonly int[] s_thicknessParameters = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        public PenCreatorDialog()
        {
            this.InitializeComponent();
            CreateColorGrid();
            this.cmb_Width.ItemsSource = s_thicknessParameters;
            this.cmb_Height.ItemsSource = s_thicknessParameters;
            this.DrawingAttributes = w_inkCanvas.DefaultDrawingAttributes;
            this.DataContext = this;
        }

        private void CreateColorGrid()
        {
            var props = typeof(Brushes).GetProperties(BindingFlags.Public | BindingFlags.Static);
            // Create individual items
            foreach (var p in props)
            {
                var b = new Button
                            {
                                Background = (SolidColorBrush) p.GetValue(null, null), 
                                Foreground = Brushes.Transparent, 
                                BorderBrush = Brushes.Transparent,
                                Height=20,
                                Width=20,
                            };
                b.Click += OnColorSelected;
                this.grid_colorPalette.Children.Add(b);
            }
        }

        private DrawingAttributes _drawingAttrib;
        public DrawingAttributes DrawingAttributes
        {
            get
            {
                return _drawingAttrib.Clone();
            }
            set
            {
                _drawingAttrib = value.Clone();
                cmb_Height.SelectedValue = (int) _drawingAttrib.Height;
                cmb_Width.SelectedValue = (int) _drawingAttrib.Width;
                w_inkCanvas.DefaultDrawingAttributes = _drawingAttrib;
            }
        }

        public bool IsHighlighter
        {
            get
            {
                return _drawingAttrib.IsHighlighter;    
            }
            set
            {
                _drawingAttrib.IsHighlighter = value;
                RefreshInkCanvas();                
            }
        }

        public int InkHeight
        {
            get
            {
                return (int)_drawingAttrib.Height;
            }
            set
            {
                _drawingAttrib.Height = value;
                RefreshInkCanvas();
            }
        }

        public int InkWidth
        {
            get
            {
                return (int) _drawingAttrib.Width;
            }
            set
            {
                _drawingAttrib.Width = value;
                RefreshInkCanvas();
            }
        }

        private void RefreshInkCanvas()
        {
            foreach (var stroke in w_inkCanvas.Strokes)
            {
                stroke.DrawingAttributes = _drawingAttrib;
            }
        }

        private void OnColorSelected(object sender, RoutedEventArgs e)
        {
            _drawingAttrib.Color = (((Button) sender).Background as SolidColorBrush).Color;
            RefreshInkCanvas();
        }

        private void HandleCancel(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void HandleOK(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }
    }
}